package edu.nuc.rubikstopwatch;

import android.database.Cursor;
import android.os.Parcel;
import android.os.Parcelable;
import edu.nuc.SQLoperation.SQLConstant;

public class Rubik implements Parcelable {
	public String id;
	public String record;
	public long millisRecord;
	public String upsetFormula = "";
	public String data;
	public long millisData;

	public Rubik(Parcel in) {
		id = in.readString();
		record = in.readString();
		millisRecord = in.readLong();
		upsetFormula = in.readString();
		data = in.readString();
		millisData = in.readLong();
	}

	public Rubik() {

	}

	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	public void writeToParcel(Parcel dest, int flags) {
		// TODO Auto-generated method stub
		dest.writeString(id);
		dest.writeString(record);
		dest.writeLong(millisRecord);
		dest.writeString(upsetFormula);
		dest.writeString(data);
		dest.writeLong(millisData);
	}

	public static final Parcelable.Creator<Rubik> CREATOR = new Parcelable.Creator<Rubik>() {

		public Rubik createFromParcel(Parcel source) {
			// TODO Auto-generated method stub
			return new Rubik(source);
		}

		public Rubik[] newArray(int size) {
			// TODO Auto-generated method stub
			return new Rubik[size];
		}

	};

	// 得到打乱公式：
	// 随机产生25步
	public String getUPsetFormula() {
		String[] s = { "U", "U'", "U2", "R", "R'", "R2", "F", "F'", "F2", "D",
				"D'", "D2", "L", "L'", "L2", "B", "B'", "B2" };
		this.upsetFormula = "";
		for (int i = 0; i < 25; i++) {
			int j = (int) (Math.random() * 17);
			this.upsetFormula = this.upsetFormula + s[j] + " ";
		}
//		System.out.println(this.upsetFormula);
		return this.upsetFormula;
	}

	public String parseMillistime(long millistime) {
		String score = null;
		int seconds = (int) (millistime / 1000);
		int millis = (int) ((millistime % 1000) / 100);
		int minutes = seconds / 60;
		seconds = seconds % 60;
		score = String.format("%02d", minutes) + ":"
				+ String.format("%02d", seconds) + "."
				+ String.format("%d", millis);
		return score;
	}

	public static Rubik parseCursor(Cursor cursor) {
		if (null == cursor || 0 == cursor.getCount()) {
			return null;
		} else if (-1 == cursor.getPosition()) {
			cursor.moveToFirst();
		}
		Rubik rubik = new Rubik();
		rubik.id = cursor.getString(cursor.getColumnIndex(SQLConstant.ID));
		rubik.record = cursor.getString(cursor.getColumnIndex(SQLConstant.SCORE));
		rubik.millisRecord = cursor.getLong(cursor.getColumnIndex(SQLConstant.MILIS_SCORE));
		rubik.upsetFormula = cursor.getString(cursor.getColumnIndex(SQLConstant.UPSETFORMULA));
		rubik.data = cursor.getString(cursor.getColumnIndex(SQLConstant.data));
		rubik.millisData = cursor.getLong(cursor.getColumnIndex(SQLConstant.MILLIS_DATA));
		return rubik;
	}

}
